﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bussiness.Loan;
using System.Data;
using FOModels.Loan;
using FOModels.Company;
using System.Net;
using Bussiness.Company;
using Bussiness.Language;
using FOModels.Language;
using Bussiness.Product;
using FOSystem.CultureHelper;
namespace FO.Controllers
{
    public class LoanController : CultureController
    {
        //
        // GET: /Loan/
        public ActionResult Index()
        {
            LoanBLL bll = new LoanBLL();
            ProductBLL probll = new ProductBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int LoanID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["LoanID"].ToString());
                ViewBag.LoanPlan = bll.getLoanPlan();
                ViewBag.LoanCate = bll.getLoanCate();
                ViewBag.LoanDefault = bll.getdefaultloan(1, 1);
                ViewBag.ProDes = probll.getProductByID(LoanID, lang.LangID);
           
            return View();
        }
         [Authorize(Roles = "8,9")]
        public ActionResult ShowCase()
        {
            return View();
        }
         [Authorize(Roles = "8,9")]
        public ActionResult ShowCaseChild()
        {
            return View();
        }
        [HttpPost]
        public ActionResult getLoan(int cate, int planid, string balance)
        {
            LoanBLL bll = new LoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<LoanModels> list = new List<LoanModels>();
            if (lang.LangID == 1)
            {
                list = bll.getloan(cate, planid, balance);
            }
            else
            {
                list = bll.getloan_EN(cate, planid, balance);
            }
            return Json(list);
        }

        public ActionResult LoanInfo(int LoanID)
        {
            LoanBLL bll = new LoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LoanModels list = new LoanModels();
            if (lang.LangID == 1)
            {
                list = bll.getloanbyid(LoanID);
            }
            else
            {
                list = bll.getloanbyid_EN(LoanID);
            }
            return View(list);
        }

        [Authorize(Roles = "8,9")]
        public ActionResult ManagerLoan()
        {
            return View();
        }
        public JsonResult getLoanJson()
        {
            LoanBLL cardbll = new LoanBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<LoanModels> list = new List<LoanModels>();
            if (lang.LangID == 1)
            {
                list = cardbll.getloanjson();
            }
            else
            {
                list = cardbll.getloanjson_EN();
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateLoan(int LoanID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LoanBLL bll = new LoanBLL();
            List<LoanCategoryModels> cate = new List<LoanCategoryModels>();
            List<LoanPlanCategoryModels> plan = new List<LoanPlanCategoryModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            LoanModels saving = new LoanModels();
            plan = bll.getLoanPlan();
            cate = bll.getLoanCate();
            com = company.getCompany(lang.LangID);
            saving = bll.getloanbyid(LoanID);
            ViewBag.LoanCateListID = new SelectList(cate, "LoanCategoryID", "LoanCategoryName", saving.LoanCategoryID);
            ViewBag.LoanPlanListID = new SelectList(plan, "LoanPlanCategoryID", "LoanPlanCategoryName", saving.LoanPlanCategoryID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", saving.CompanyID);
            Models.Loan sa = new Models.Loan();
            sa.LoanName = saving.LoanName;
            sa.CompanyID = saving.CompanyID;
            sa.MinimumBalance = saving.MinimumBalance;
            sa.MaximumBalance = saving.MaximumBalance;
            sa.InterestRate = saving.InterestRate;
            sa.Description = WebUtility.HtmlDecode(saving.Description);
            sa.Benefit = WebUtility.HtmlDecode(saving.Benefit);
            sa.LoanCategoryID = saving.LoanCategoryID;
            sa.LoanPlanCategoryID = saving.LoanPlanCategoryID;
            return View(sa);
        }

        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateLoan(Models.Loan de)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LoanBLL bll = new LoanBLL();
            List<LoanCategoryModels> cate = new List<LoanCategoryModels>();
            List<LoanPlanCategoryModels> plan = new List<LoanPlanCategoryModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            LoanModels saving = new LoanModels();
            cate = bll.getLoanCate();
            plan = bll.getLoanPlan();
            com = company.getCompany(lang.LangID);
            saving = bll.getloanbyid(de.LoanID);

            ViewBag.LoanCateListID = new SelectList(cate, "LoanCategoryID", "LoanCategoryName", saving.LoanCategoryID);
            ViewBag.LoanPlanListID = new SelectList(plan, "LoanPlanCategoryID", "LoanPlanCategoryName", saving.LoanPlanCategoryID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", saving.CompanyID);
            if (saving != null)
            {
                saving.LoanName = de.LoanName;
                saving.CompanyID = de.CompanyID;
                saving.MinimumBalance = de.MinimumBalance.Replace(".", "").Replace(".", ""); ;
                saving.MaximumBalance = de.MaximumBalance.Replace(".", "").Replace(".", ""); ;
                saving.InterestRate = de.InterestRate;
                saving.Description = WebUtility.HtmlDecode(de.Description);
                saving.Benefit = WebUtility.HtmlDecode(de.Benefit);
                saving.LoanCategoryID = de.LoanCategoryID;
                saving.LoanPlanCategoryID = de.LoanPlanCategoryID;
                if (lang.LangID == 1)
                {
                    bll.UpdateLoan(saving);
                }
                else
                {
                    bll.UpdateLoan_EN(saving);
                }
                return RedirectToAction("ManagerLoan");
            }
            return View();
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertLoan()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LoanBLL bll = new LoanBLL();
            List<LoanCategoryModels> cate = new List<LoanCategoryModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            List<LoanPlanCategoryModels> plan = new List<LoanPlanCategoryModels>();

            cate = bll.getLoanCate();
            plan = bll.getLoanPlan();
            com = company.getCompany(lang.LangID);

            ViewBag.LoanCateListID = new SelectList(cate, "LoanCategoryID", "LoanCategoryName");
            ViewBag.LoanPlanListID = new SelectList(plan, "LoanPlanCategoryID", "LoanPlanCategoryName");
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");
            return View();

        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertLoan(FO.Models.Loan de)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LoanBLL bll = new LoanBLL();
            List<LoanCategoryModels> cate = new List<LoanCategoryModels>();
            List<LoanPlanCategoryModels> plan = new List<LoanPlanCategoryModels>();
            CompanyBLL company = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
        
                cate = bll.getLoanCate();
                plan = bll.getLoanPlan();
                com = company.getCompany(lang.LangID);
           
            ViewBag.LoanCateListID = new SelectList(cate, "LoanCategoryID", "LoanCategoryName");
            ViewBag.LoanPlanListID = new SelectList(plan, "LoanPlanCategoryID", "LoanPlanCategoryName");
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");

            if (ModelState.IsValid)
            {
                LoanModels saving = new LoanModels();
                saving.LoanName = de.LoanName;
                saving.CompanyID = de.CompanyID;
                saving.MinimumBalance = de.MinimumBalance.Replace(".", "").Replace(".", ""); ;
                saving.MaximumBalance = de.MaximumBalance.Replace(".", "").Replace(".", ""); ;
                saving.InterestRate = de.InterestRate;
                saving.Description = WebUtility.HtmlDecode(de.Description);
                saving.Benefit = WebUtility.HtmlDecode(de.Benefit);
                saving.LoanCategoryID = de.LoanCategoryID;
                saving.LoanPlanCategoryID = de.LoanPlanCategoryID;
                if (lang.LangID == 1)
                {
                    bll.InsertLoan(saving);
                }
                else
                {
                    bll.InsertLoan_EN(saving);
                }
                return RedirectToAction("ManagerLoan");
            }
            return View(de);

        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteLoan(int LoanID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            LoanBLL bll = new LoanBLL();
            LoanModels del = new LoanModels();
            if (lang.LangID == 1)
            {
                del = bll.getloanbyid(LoanID);
                if (del != null)
                {
                    bll.DeleteLoan(del.LoanID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                del = bll.getloanbyid_EN(LoanID);
                if (del != null)
                {
                    bll.DeleteLoan_EN(del.LoanID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }

        }
    }
}
